<template>
    <div class="flex flex-col h-full overflow-hidden">
        <div v-if="pager.lists.length > 0" class="flex-1">
            <el-scrollbar>
                <Waterfall
                    ref="waterFull"
                    :delay="100"
                    :list="pager.lists"
                    :width="326"
                    :gutter="20"
                    :animationDelay="0"
                    :animationDuration="0"
                    backgroundColor="none"
                    animationPrefix="none"
                    animated="none"
                    animationEffect="none"
                    :breakpoints="breakpoints"
                >
                    <template #item="scope: { item: SquareDrawsResponse }">
                        <div class="image-payload-container">
                            <photos-cover
                                :thumbnail="scope.item.thumbnail"
                                :image="scope.item?.image_loc"
                                @on-click="(val) => (previewLists = val)"
                            />
                            <div class="image-icons top-[10px] left-[10px]">
                                <icon
                                    v-if="!scope.item.is_collect"
                                    name="el-icon-Star"
                                    color="#ffffff"
                                    :size="16"
                                    @click="handleCollect(scope.item.code, scope.item.is_collect)"
                                />
                                <icon
                                    v-else
                                    name="el-icon-StarFilled"
                                    color="#fd5656"
                                    :size="20"
                                    @click="handleCollect(scope.item.code, scope.item.is_collect)"
                                />
                            </div>
                            <el-tooltip effect="dark" content="查看图片" placement="top">
                                <div
                                    class="image-icons top-[10px] right-[10px]"
                                    @click.stop="handlePoster(scope.item)"
                                >
                                    <icon
                                        name="el-icon-Picture"
                                        color="#ffffff"
                                        :size="16" />
                                </div>
                            </el-tooltip>
                            <div class="image-content">
                                <p class="text-white line-clamp-2">
                                    {{ scope.item?.prompt_in || scope.item?.prompt_en }}
                                </p>
                                <div class="flex justify-between mt-2.5">
                                    <div class="flex items-center">
                                        <template v-if="configs.is_user">
                                            <ElAvatar :size="28" :src="scope.item.user?.avatar" />
                                            <p class="ml-[6px] w-20 text-white truncate">
                                                {{ scope.item.user?.nickname }}
                                            </p>
                                        </template>
                                    </div>
                                    <div class="flex items-center" @click="copy(scope.item.prompt_in)">
                                        <icon name="el-icon-CopyDocument" color="#ffffff" />
                                        <span class="ml-1.5 text-white">复制</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </template>
                </Waterfall>
            </el-scrollbar>
        </div>

        <div
            v-if="pager.lists.length <= 0 && !firstLoading"
            class="flex flex-col justify-center h-full"
        >
            <el-empty class="!p-0" description="空空如也~" />
        </div>

        <div
            v-if="pager.lists.length > 0 && !firstLoading"
            class="flex justify-center my-4"
        >
            <paging
                v-model="pager"
                layout="prev, pager, next"
                @change="queryLists"
            />
        </div>

        <el-image-viewer
            v-if="previewLists.length"
            :url-list="previewLists"
            :hide-on-click-modal="true"
            @close="previewLists = []"
        />

        <popup-poster-draws
            ref="popupPosterRef"
            :is-user="!!configs.is_user"
        />
    </div>
</template>

<script setup lang="ts">
import { watchDebounced } from '@vueuse/core'
import { popupEnum } from '~/enums/app'
import useAppStore from '~/stores/app'
import useUserStore from '~/stores/user'
import squareApi from '~/api/square'
import drawingApi from '~/api/apply/draws/drawing'
import PopupPosterDraws from '../_popup/poster-draws.vue'

const props = withDefaults(
    defineProps<{
        cateId?: number;
        keyword?: string;
        configs?: SquareSettingResponse;
    }>(),
    {
        cateId: 0,
        keyword: '',
        configs: {} as SquareSettingResponse | any
    }
)

const { copy } = useCopy()
const appStore = useAppStore()
const userStore = useUserStore()
const popupPosterRef = shallowRef()

// 首次加载状态
const firstLoading = ref<boolean>(true)

// 图片预览列表
const previewLists = ref<string[]>([])

// 行元素断点值
const breakpoints: Record<number, { rowPerView: number }> = {
    4000: { rowPerView: 7 },
    2000: { rowPerView: 6 },
    1800: { rowPerView: 5 },
    1600: { rowPerView: 5 },
    1440: { rowPerView: 4 },
    1360: { rowPerView: 4 },
    1280: { rowPerView: 4 },
    1024: { rowPerView: 3 }
}

// 查询参数
const queryParams = reactive({
    cate: props.cateId,
    keyword: props.keyword
})

// 分页查询
const { pager, queryLists, resetPaging } = usePaging({
    fetchFun: squareApi.draws,
    params: queryParams
})

/**
 * 生成海报
 *
 * @param {SquareDrawsResponse} value
 * @author zero
 */
const handlePoster = (value: SquareDrawsResponse) => {
    if (!userStore.isLogin) {
        return appStore.setPopup(popupEnum.LOGIN)
    }

    popupPosterRef.value.open(value)
}

/**
 * 收藏记录
 *
 * @param {string} code
 * @param {boolean} status
 * @author zero
 */
const handleCollect = async (code: string, status: boolean) => {
    if (!userStore.isLogin) {
        return appStore.setPopup(popupEnum.LOGIN)
    }

    await drawingApi.collect(code)
    await queryLists()
    if (status) {
        feedback.msgSuccess('取消成功')
    } else {
        feedback.msgSuccess('收藏成功')
    }
}

watch(
    () => props.cateId,
    async () => {
        queryParams.cate = props.cateId
        queryParams.keyword = props.keyword
        await queryLists()
        firstLoading.value = false
    },
    { immediate: true }
)

watchDebounced(
    () => props.keyword,
    async (value) => {
        queryParams.keyword = value
        await resetPaging()
    },
    {
        debounce: 500
    }
)
</script>

<style scoped lang="scss">
.image-payload-container {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-size: 13px;
    cursor: pointer;
    border-radius: 12px;
    &:hover {
        .image-content { bottom: 0; opacity: 1; }
        .image-icons { opacity: 1; }
    }
    .image-icons {
        position: absolute;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 32px;
        height: 32px;
        background-color: rgb(0 0 0 / 50%);
        border-radius: 32px;
        opacity: 0;
        transition: opacity 0.5s;
    }
    .image-content {
        position: absolute;
        right: 0;
        bottom: -10px;
        left: 0;
        padding: 10px;
        margin: 10px;
        background-color: rgb(0 0 0 / 50%);
        border-radius: 6px;
        opacity: 0;
        transition: all 0.5s;
    }
}
</style>
